/*品牌管理-----------------------------------------------------------------------------------*/
var comPageNum = 1
function brand_list(pageNumber) {
    comPageNum = pageNumber
    var dataSource = new Array();
    //table的数据源
    var table; //table对象
    dataSource = [];
    var user_data = {
        brandName: $("#brandName").val(),
        entryTimeStart:$("#logmin").val(),
        entryTimeEnd:$("#logmax").val(),
        show_status: $("#STUTAS").val(),
        pageNumber: pageNumber,
        pageSize: 50
    }
    var succCallBack = function(data, status, response) {
        console.log(data.data)
        if(data.errorCode == "0") {
            var totalpage = data.data.totalPage;
            $('.M-box3').pagination({
                pageCount: totalpage,
                current:comPageNum,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function(api) {
                    comPageNum = api.getCurrent();
                    brand_list(api.getCurrent());
                }
            });
            $.each(data.data.list, function(i, field) {
                var date = new Date(field.creation_date);
                Y = date.getFullYear() + '-';
                M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
                D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
                h = (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
                m = (date.getMinutes() < 10 ? '0'+(date.getMinutes()): date.getMinutes())+ ':';
                s = date.getSeconds()< 10 ? '0'+(date.getSeconds()) : date.getSeconds();

                var rowData = new Array();
                rowData.push(field.logo);
                rowData.push(field.brand_name);
                rowData.push(field.brand_english_name);
                rowData.push(field.brand_code);
                rowData.push(field.parent_brand_name);
                rowData.push(field.show_status);
                rowData.push(field.brand_id);
                rowData.push(Y+M+D+h+m+s);

                dataSource.push(rowData);
            });
            initDataTable(dataSource);
            console.log(dataSource)

            function initDataTable(dataSource) {
                var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
                dttable.fnClearTable(); //清空一下table
                dttable.fnDestroy(); //还原初始化了的datatable
                $("#datatable").DataTable({
                    "order": [[ 4, "desc" ]], // 排序
                    "autoWidth": false,
                    "ordering": true,
                    "searching": false,
                    "paging": false,
                    "retrieve": true,
                    "destroy": true,
                    "bAutoWidth": true,
                    "bLengthChange": true,
                    "aLengthMenu": [50, 25, 10, 5],
                    "oLanguage": {
                        "sLengthMenu": "每页显示 _MENU_ 条记录",
                        "sZeroRecords": "抱歉， 没有找到",
                        "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                        "sInfoEmpty": "没有数据",
                        "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                        "oPaginate": {
                            "sFirst": "首页",
                            "sPrevious": "前一页",
                            "sNext": "后一页",
                            "sLast": "尾页"
                        },
                        "sZeroRecords": "没有检索到数据",
                        "sProcessing": "<img src='./loading.gif' />"
                    },
                    data: dataSource,
                    columnDefs: [{
                        "visible": true,
                        "targets": 0
                    },
                        {
                            "render": function(data, type, row, meta) {
                                var val = '<img onclick="picShow(\'查看图片\',\'picShow.html\',\'' + row[0] + '\')" title="" width="100" height="50" src="' + row[0] + '" id="myImg" class="mr-10">';
                                return val;
                            },
                            "targets": 0
                        },
                        {
                            "render": function (data, type, row, meta) {
                                var val = row[1]
                                if (val == undefined) {
                                    val = ''
                                }
                                return val;
                            },
                            "targets": 1
                        },
                        {
                            "render": function (data, type, row, meta) {
                                var val = row[2]
                                if (val == undefined) {
                                    val = ''
                                }
                                return val;
                            },
                            "targets": 2
                        },
                        {
                            "render": function (data, type, row, meta) {
                                var val = row[3]
                                if (val == undefined) {
                                    val = ''
                                }
                                return val;
                            },
                            "targets": 3
                        },

                        {
                            "render": function (data, type, row, meta) {
                                var val = row[4]
                                if (val == undefined) {
                                    val = ''
                                }
                                return val;
                            },
                            "targets": 4
                        },


                        {
                            "render": function(data,type,row,meta){
                                /*if(row[3]=="0"){
                                    var val = '<span class="dropDown"> <a class="dropDown_A powerButton 411" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">开启<i class="Hui-iconfont">&#xe6df;</i></a><ul class="dropDown-menu menu radius box-shadow"><li onclick="brand_stop(\'\',\''+ row[4] +'\')"><a href="#">禁用</a></li></ul></span>';
                                    return val;
                                }else{
                                    var val = '<span class="dropDown"> <a class="dropDown_A powerButton 411" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">禁用<i class="Hui-iconfont">&#xe6df;</i></a><ul class="dropDown-menu menu radius box-shadow"><li onclick="brand_start(\'\',\''+ row[4] +'\')"><a href="#">开启</a></li></ul></span>';
                                    return val;
                                }*/
                                //关闭操作按钮
                                if(row[5]=="0"){
                                    var val = '启用';
                                    return val;
                                }else{
                                    var val = '禁用';
                                    return val;
                                }
                            },
                            "targets": 5
                        },
                        {
                            "render": function(data, type, row, meta) {
                                if(data == null){
                                    var val ="";
                                    return val;
                                }else {
                                    var val = '<span>'+row[7]+'</span><br>';
                                    return val;
                                }
                            },
                            "targets": 6
                        },
                       /* {
                            "render": function(data, type, row, meta) {
                                var val = '<a><span class="list powerButton 1000001004725699" onclick="editBrand(\''+ row[0] + '\',\''+ row[0]+ '\',\'' + row[2] + '\',\'' + row[4] + '\')">编辑</span>&nbsp;&nbsp;<span class="list powerButton 1000000884912873" onclick="deleteBrand(\'' + row[4] + '\')">删除</span></a>';
                                return val;
                            },
                            "targets": 5
                        },*/
                    ]
                });
                power();
            };
        }
        power();
    };
    return web.doAjax(root.goodsInter.goods_getBrandByCodeAndName, 'post', user_data, succCallBack);
}
/*点击查看大图*/
function picShow(title, url, imgUrl) {
    console.log('title:'+title)
    console.log('url:'+url)
    console.log('imgUrl:'+imgUrl)

    window.event ? window.event.cancelBubble = true : e.stopPropagation();
    sessionStorage.setItem("imgUrl", imgUrl);
    layer_show(title, url, "800", "500");
}

function addBrand() {
    layer_show("添加品牌", "addBrand.html", 669, 445);
}

function editBrand(brand_name,logo,description,brand_id) {
    console.log(description)
    var dataObj = {
        "brand_name": brand_name,
        "logo": logo,
        "description": description,
        "brand_id": brand_id,
    }
    sessionStorage.setItem("brandData", JSON.stringify(dataObj))
    layer_show("编辑品牌", "editBrand.html", 669, 445);
}

/*删除*/
function deleteBrand(id) {
    var localStorageParameters = web.readCookie("loginData");
    var JsonObject =JSON.parse(localStorageParameters);
    // 校验是否已经被商品使用
    $.ajax({
        type: "POST",
        url: root.goodsInter.goods_deleteBrand,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            brandId:id,
            lastModifiedBy:JsonObject.user.user_ID,
            type : 0
        },  //传入组装的参数
        success: function (data) {
            if(data.errorCode != 0){
                layer.msg(data.errorMessage, {icon: 2, time: 2000});
            }else{
                layer.confirm('确认删除该品牌？',function () {
                    $.ajax({
                        type: "POST",
                        url: root.goodsInter.goods_deleteBrand,
                        //url:"http://192.168.65.110:7050/aproduct/brand/delBrand",
                        cache: false,  //禁用缓存
                        dataType: "json",
                        data: {
                            brandId:id,
                            lastModifiedBy:JsonObject.user.user_ID,
                            type :1
                        },  //传入组装的参数
                        success: function (data) {
                            switch (data.errorCode) {
                                case 1010031:
                                    layer.msg(data.errorMessage, {icon: 2, time: 2000});
                                    break;
                                case 0:
                                    layer.msg("删除成功", {icon: 1, time: 1000});
                                    brand_list(comPageNum);
                                    cancelLayerNoRefreshFn();
                                    break;
                            }
                        },
                    });
                })
            }
        },
    });
}

/*停用*/
function brand_stop(obj,id){
    var localStorageParameters = decodeURIComponent(web.readCookie("loginData"));
    var JsonObject =JSON.parse(localStorageParameters);
    layer.confirm('确认要停用吗？',function(index){
        console.log(id)
        $.ajax({
            type: "POST",
            url: root.goodsInter.goods_editBrandStatus,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                brandId: id,
                show_status: "1",
                lastModifiedBy:JsonObject.user.user_ID
            },  //传入组装的参数
            success: function (data) {
                if (data.errorCode==0) {
                    layer.msg("停用成功", {icon: 2, time: 2000});
                    brand_list(comPageNum);
                    cancelLayerNoRefreshFn();
                }
            },
        });
        layer.msg('已停用!', {icon: 6,time:1000});
    });
}
/*启用*/
function brand_start(obj,id){
    var localStorageParameters = decodeURIComponent(web.readCookie("loginData"));
    var JsonObject =JSON.parse(localStorageParameters);
    layer.confirm('确认要启用吗？',function(index){
        $.ajax({
            type: "POST",
            url: root.goodsInter.goods_editBrandStatus,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                brandId: id,
                show_status: "0",
                lastModifiedBy:JsonObject.user.user_ID
            },  //传入组装的参数
            success: function (data) {
                if (data.errorCode==0) {
                    layer.msg("启用成功", {icon: 2, time: 2000});
                    brand_list(comPageNum);
                    cancelLayerNoRefreshFn();
                }
            },
        });
        layer.msg('已启用!', {icon: 6,time:1000});
    });
}